package com.bawei.es.controller;



import com.bawei.common.core.domain.R;
import com.bawei.es.service.EsService;
import com.bawei.system.domain.vo.IndexInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author：sxq
 * @Package：com.bawei.es.controller
 * @Project：mall_cloud
 * @name：EsController
 * @Date：2024/6/20 11:26
 */
@RestController
@RequestMapping("/esIndex1")
@Api(value = "ES索引管理",tags = "ES索引管理")
public class EsController {

    @Autowired
    private EsService esService;



    /**
     * 根据索引名称创建索引
     *
     * @return
     */
    @PostMapping("/create")
    @ApiOperation(value = "根据索引名称创建索引")
    public R<Boolean> indexCreate(@RequestBody IndexInfo indexInfo) {
        return esService.indexCreate(indexInfo);
    }


    /**
     * 根据索引名称创建索引
     *
     * @return
     */
    @GetMapping("/byImages/{productId}")
    @ApiOperation(value = "根据索引名称查询图片集")
    public R<List<Map<String, Object>>> byImages(@PathVariable("productId") Integer productId) {
        List<Map<String, Object>> maps = esService.byImages(productId);
        return R.ok(maps);
    }


    @GetMapping("/indexDelete")
    @ApiOperation(value = "根据索引名称删除索引")
    public R indexDelete(String indexName) {
         esService.indexDelete(indexName);
         return R.ok("删除成功");
    }




    @GetMapping("/byIndexName")
    @ApiOperation(value = "根据索引名称得到详情")
    public R<IndexInfo> byIndexName(String indexName) {
        IndexInfo indexInfo = esService.byIndexName(indexName);
        return R.ok(indexInfo);

    }


    @PostMapping("/updIndex")
    @ApiOperation(value = "根据索引名称得到详情")
    public R<IndexInfo> updIndex(@RequestBody IndexInfo indexInfo) {
        IndexInfo indexInfo1 = esService.updIndex(indexInfo);
        return R.ok(indexInfo1);

    }


    @PostMapping("/updateWithMappingsAndSettings")
    @ApiOperation(value = "根据索引名称修改索引mapping映射")
    public R updateMapping(@RequestBody IndexInfo esIndexInfo, @RequestParam Integer flag) {
        esService.updateMapping(esIndexInfo, flag);
        return R.ok();
    }

    /**
     * 根据索引名称获取mapping
     *
     * @return
     */
    @GetMapping("/indexGetMapping")
    @ApiOperation(value = "根据索引名称查询索引配置和映射")
    public R<IndexInfo> indexGetSettingAndMapping(@RequestParam String indexName) {
        return R.ok(esService.indexGetSettingAndMapping(indexName));
    }


}
